﻿namespace SourceCodeRepository.Web.ViewDataTransformers
{
    using System.Collections.Generic;
    using Common.Interfaces.Model;
    using Views.ViewData;

    /// <summary>
    /// View Data Transformer.
    /// </summary>
    /// <typeparam name="TViewData">The view data class to be transformed.</typeparam>
    /// <typeparam name="TModelEntity">The entity class to transform to.</typeparam>
    public abstract class ViewDataTransformer<TViewData, TModelEntity>
        where TViewData : IViewData, new()
        where TModelEntity : IDomainObject
    {
        /// <summary>
        /// Gets the transforms.
        /// </summary>
        /// <value>The transforms.</value>
        protected abstract List<Transformer> Transforms { get; }

        public delegate void Transformer(TModelEntity entity, TViewData viewData);

        /// <summary>
        /// Transforms the specified entity to the specified view data.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public TViewData Transform(TModelEntity entity)
        {
            var viewData = new TViewData();
            Transforms.ForEach((item) => item(entity, viewData));
            return viewData;
        }

        /// <summary>
        /// Transforms the specified entities.
        /// </summary>
        /// <param name="entities">The entities.</param>
        /// <returns></returns>
        public IList<TViewData> Transform(IList<TModelEntity> entities)
        {
            var list = new List<TViewData>();

            foreach (var item in entities)
            {
                list.Add(this.Transform(item));
            }
            return list;
        }
    }
}